<?php
switch($submenu)
{
	default:
?>
	<h2>Customer</h2>
	<table class="rounded-corner">
		<thead>
			<tr>
				<th scope="col" class="rounded-company"></th>
				<th scope="col" class="rounded">Kode</th>
				<th scope="col" class="rounded">Nama</th>
				<th scope="col" class="rounded-q4">Action</th>
			</tr>
		</thead>
		<tbody>
<?php
	$page = new Paging;
	$batas = 10;
	$posisi = $page->cariPosisi($batas);
	$get_customer = mysql_query("SELECT * FROM mcustomer WHERE intStatus = 1 ORDER BY vcKode LIMIT $posisi, $batas");
	$baris = mysql_num_rows(mysql_query("SELECT * FROM mcustomer WHERE intStatus = 1"));
	$halaman = $page->jumlahHalaman($baris,$batas);
	$link = $page->navHalaman($_GET["p"],$halaman);
	$no = $posisi+1;
	while($r = mysql_fetch_array($get_customer))
	{
		echo"
		<tr>
		<td>$no</td>
		<td>".$r["vcKode"]."</td>
		<td>".$r["vcNama"]."</td>
		<td><a href=\"?m=$menu&md=setting&id=".$r["intNomor"]."\"><img src=\"../images/user_edit.png\"></a></td>
		</tr>";
		$no++;
	}
?>
		</tbody>
	</table>
<?php
	include "../php/pagination.php";
	break;
	
	case "setting":
	$get_customer = mysql_fetch_array(mysql_query("SELECT * FROM mcustomer WHERE intNomor = ".$_GET["id"]));
	echo "<h2>".$get_customer["vcNama"]." (".$get_customer["vcKode"].")</h2>";
?>
	<form action="?m=home&md=save" class="niceform" method="post">
	<input name="id" size="30" type="hidden" value="<?php echo $get_customer["intNomor"]; ?>" />
	<table class="rounded-corner">
		<thead>
			<tr>
				<th scope="col" class="rounded-company"></th>
				<th scope="col" class="rounded">Brand</th>
				<th scope="col" class="rounded">Diskon (%)</th>
				<th scope="col" class="rounded-q4"></th>
			</tr>
		</thead>
		<tbody>
<?php
	$get_brand = mysql_query("SELECT * FROM mbrand WHERE intStatus = 1 ORDER BY vcNama");
	$no = 1;
	while($r = mysql_fetch_array($get_brand))
	{
		$get_discount = mysql_query("SELECT * FROM web_discount WHERE customer_id = ".$get_customer["intNomor"]." AND brand_id = ".$r["intNomor"]." AND status = 1");
		$check_discount = mysql_num_rows($get_discount);
		if($check_discount > 0)
		{
			$s = mysql_fetch_array($get_discount);
			$discount = $s["discount_rate"];
		}
		else
			$discount = 0;
		echo"
		<tr>
		<td>$no</td>
		<td>".$r["vcNama"]."</td>
		<td><input class=\"discount_procentage\" name=\"discount".$r["intNomor"]."\" size=\"2\" type=\"text\" value=\"$discount\" /></td>
		<td></td>
		</tr>";
		$no++;
	}
?>
		</tbody>
	</table>
	<div class="form">
		<fieldset>
			<dl class="submit"><input id="submit" name="submit" type="submit" value="Simpan" /></dl>
		</fieldset>
	</div>
	<table><tr><td>
		<div class="form">
			<fieldset>
				<dl>
					<dt><label for="nama">Diskon <i>default</i>&nbsp;:&nbsp;</label></dt>
					<dd>
						<input id="discount_default" name="" size="30" type="text" />
						<input id="discount_button" name="" type="button" value="Ubah semua" />
					</dd>
				</dl>
			</fieldset>
		</div>
	</td></tr></table>
	
	</form>
<?php
	break;
	
	case "save":
	if(!empty($_POST["id"]))
	{
		$admin_id = $_SESSION["login.id"];
		$customer_id = $_POST["id"];
		$wipe = mysql_query("UPDATE web_discount SET status = 0, date_deleted = now(), deleted_by = $admin_id WHERE customer_id = $customer_id");
		if($wipe)
		{
			$get_brand = mysql_query("SELECT * FROM mbrand WHERE intStatus = 1 ORDER BY vcNama");
			$failed = 0;
			$forbidden = 0;
			while($r = mysql_fetch_array($get_brand))
			{
				$brand_id = $r["intNomor"];
				$discount = $_POST["discount$brand_id"];
				if(!ctype_digit($discount))
				{
					$discount = 0;
					$forbidden++;
				}
				$save = mysql_query("INSERT INTO web_discount (customer_id,brand_id,discount_rate,date_created,created_by) 
									 VALUES ($customer_id,$brand_id,$discount,now(),$admin_id)");
				if(!$save)
					$failed++;
			}
			if($failed > 0)
			{
				$_SESSION["$menu.warning_box"] = "error";
				$_SESSION["$menu.warning_msg"] = "Proses penyimpanan setting diskon <i>corupt</i> (<u>tidak sempurna</u>), silahkan memeriksa ulang apakah setting diskon sudah benar";
			}
			elseif($forbidden > 0)
			{
				$_SESSION["$menu.warning_box"] = "warning";
				$_SESSION["$menu.warning_msg"] = "Setting diskon berhasil disimpan, namun ditemukan setting yang menggunakan karakter <i>non numeric</i> (dikonversi menjadi 0%)";
			}
			else
			{
				$_SESSION["$menu.warning_box"] = "valid";
				$_SESSION["$menu.warning_msg"] = "Setting diskon telah disimpan";
			}
		}
		else
		{
			$_SESSION["$menu.warning_box"] = "error";
			$_SESSION["$menu.warning_msg"] = "Setting diskon gagal disimpan >> setting diskon yang lama tidak bisa diakses";
		}
	}
	die("<meta content='0;URL=?m=$menu&setting&id=$customer_id' http-equiv='refresh' />");
	break;
}
?>